<?php

namespace App\Http\Controllers;

use App\Utils\Weixin\WeixinUtils;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use App\Utils\Weixin\WechatRedisCache;
use Illuminate\Support\Facades\Config;


use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Jenssegers\Agent\Facades\Agent;

class WxController extends Controller
{

    private $wxClient;

    public function __construct()
    {

        $this->wxClient = new WechatRedisCache([
            "appid" => config("weChat.appid"),
            "appsecret" => config("weChat.appsecret"),
        ]);
    }

    /* 现金购买 */
    public function wxHome(Request $request, $buyVip = '')
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
//        sort($products);
        $paras = [
            'signature' => $signature,
            'products' => $products,
        ];

        $fromDate = Carbon::createFromFormat('Y-m-d', '2016-01-01');
        $paras['largeLimit'] = self::largeLimit();
//
        $paras['batch'][1] = Carbon::now()->startOfWeek()->diffInWeeks($fromDate->startOfWeek());

        $paras['product'][1] = [
            'id' => 1,
            'name' => '充值100元',
            'left' => 500 - round(478 * (Carbon::now()->diffInSeconds(Carbon::now()->startOfWeek())) / (7 * 24 * 3600)),
        ];

        $paras['batch'][3] = Carbon::now()->startOfMonth()->diffInMonths($fromDate->startOfMonth()) * 2 + intval(Carbon::now()->day > 16);
        $left = 500;
        if (isset($paras['largeLimit']['start']) && isset($paras['largeLimit']['end'])) {
            $dayStart = $paras['largeLimit']['start'];
            $dayEnd = $paras['largeLimit']['end'];
            $left = 500 - round(478 * (Carbon::now()->diffInSeconds($dayStart)) / ($dayEnd->diffInSeconds($dayStart)));
        }

        $paras['product'][3] = [
            'id' => 3,
            'name' => '充值3500元',
            'left' => $left,
        ];

        if ($buyVip == 'vip') {
            $paras['recommendPlan'] = 3;
            $paras['buyOption'] = 1;

        } else if ($buyVip == 'recommend') {
            $paras['recommendPlan'] = 1;
            $paras['buyOption'] = 2;
        } else {
            $paras['recommendPlan'] = 1;
            $paras['buyOption'] = 0;
        }

        return view('mobile.home')->with($paras);
    }

    // 百度品牌专区
    public function brand(Request $request, $buyVip = '')
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        //        sort($products);
        $paras = [
            'signature' => $signature,
            'products' => $products,
        ];

        $fromDate = Carbon::createFromFormat('Y-m-d', '2016-01-01');
        $paras['largeLimit'] = self::largeLimit();
        //
        $paras['batch'][1] = Carbon::now()->startOfWeek()->diffInWeeks($fromDate->startOfWeek());

        $paras['product'][1] = [
            'id' => 1,
            'name' => '充值100元',
            'left' => 500 - round(478 * (Carbon::now()->diffInSeconds(Carbon::now()->startOfWeek())) / (7 * 24 * 3600)),
        ];

        $paras['batch'][3] = Carbon::now()->startOfMonth()->diffInMonths($fromDate->startOfMonth()) * 2 + intval(Carbon::now()->day > 16);
        $left = 500;
        if (isset($paras['largeLimit']['start']) && isset($paras['largeLimit']['end'])) {
            $dayStart = $paras['largeLimit']['start'];
            $dayEnd = $paras['largeLimit']['end'];
            $left = 500 - round(478 * (Carbon::now()->diffInSeconds($dayStart)) / ($dayEnd->diffInSeconds($dayStart)));
        }

        $paras['product'][3] = [
            'id' => 3,
            'name' => '充值3500元',
            'left' => $left,
        ];

        if ($buyVip == 'vip') {
            $paras['recommendPlan'] = 3;
            $paras['buyOption'] = 1;

        } else if ($buyVip == 'recommend') {
            $paras['recommendPlan'] = 1;
            $paras['buyOption'] = 2;
        } else {
            $paras['recommendPlan'] = 1;
            $paras['buyOption'] = 0;
        }

        return view('mobile.brand')->with($paras);
    }

    public function verifyPayment(Request $request, $planId)
    {
        $productType = Config::get('products.' . $planId);
        $planDetail = Config::get('products.' . $productType)[$planId];
        $campUrl = $request->input('campUrl');

        $billId = $request->input('out_trade_no');
        $path = "/s/bills/$billId";
        if (strlen($billId) > 8) {
            $path = "/s/cashBilling/bills/$billId";
        }
        return view('mobile.order.verifyPayment')->with('planDetail', $planDetail)
            ->with('billId', $billId)
            ->with('path', $path)
            ->with('campUrl', $campUrl);
    }

    /* 课时购买 */
    public function wxHome1(Request $request, $buyVip = '')
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
//        sort($products);
        $paras = [
            'signature' => $signature,
            'products' => $products,
        ];

        $fromDate = Carbon::createFromFormat('Y-m-d', '2016-01-01');
//
        $paras['batch'][4] = Carbon::now()->startOfWeek()->diffInWeeks($fromDate->startOfWeek());
        $product = $this->fetchProduct(4);
        if (!empty($product)) {
            $paras['product'][4] = [
                'id' => $product['id'],
                'name' => $product['name'],
                'left' => max(22, 500 - max(100, $product['totalSoldCount'] % 500))
            ];
            $product = '';
        }

        $paras['batch'][6] = Carbon::now()->startOfMonth()->diffInMonths($fromDate->startOfMonth()) * 2 + intval(Carbon::now()->day > 16);
        $product = $this->fetchProduct(6);
        if (!empty($product)) {
            $paras['product'][6] = [
                'id' => $product['id'],
                'name' => $product['name'],
                'left' => max(22, 500 - max(100, $product['totalSoldCount'] % 500))
            ];
        }
        $paras['largeLimit'] = $this->largeLimit();

        if ($buyVip == 'vip') {
            $paras['recommendPlan'] = 6;
            $paras['buyOption'] = 1;

        } else if ($buyVip == 'recommend') {
            $paras['recommendPlan'] = 4;
            $paras['buyOption'] = 2;
        } else {
            $paras['recommendPlan'] = 4;
            $paras['buyOption'] = 0;
        }

        if ($buyVip == 'branding') {
            return view('mobile.branding')->with($paras);
        }

        return view('mobile.home1')->with($paras);
    }

    // 百度品牌专区
    public function brand1(Request $request, $buyVip = '')
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
//        sort($products);
        $paras = [
            'signature' => $signature,
            'products' => $products,
        ];

        $fromDate = Carbon::createFromFormat('Y-m-d', '2016-01-01');
//
        $paras['batch'][4] = Carbon::now()->startOfWeek()->diffInWeeks($fromDate->startOfWeek());
        $product = $this->fetchProduct(4);
        if (!empty($product)) {
            $paras['product'][4] = [
                'id' => $product['id'],
                'name' => $product['name'],
                'left' => max(22, 500 - max(100, $product['totalSoldCount'] % 500))
            ];
            $product = '';
        }

        $paras['batch'][6] = Carbon::now()->startOfMonth()->diffInMonths($fromDate->startOfMonth()) * 2 + intval(Carbon::now()->day > 16);
        $product = $this->fetchProduct(6);
        if (!empty($product)) {
            $paras['product'][6] = [
                'id' => $product['id'],
                'name' => $product['name'],
                'left' => max(22, 500 - max(100, $product['totalSoldCount'] % 500))
            ];
        }
        $paras['largeLimit'] = $this->largeLimit();

        if ($buyVip == 'vip') {
            $paras['recommendPlan'] = 6;
            $paras['buyOption'] = 1;

        } else if ($buyVip == 'recommend') {
            $paras['recommendPlan'] = 4;
            $paras['buyOption'] = 2;
        } else {
            $paras['recommendPlan'] = 4;
            $paras['buyOption'] = 0;
        }

        if ($buyVip == 'branding') {
            return view('mobile.branding')->with($paras);
        }

        return view('mobile.brand1')->with($paras);
    }

    public function verifyPayment1(Request $request, $planId)
    {
        $productType = Config::get('products.' . $planId);
        $planDetail = Config::get('products.' . $productType)[$planId];
        $campUrl = $request->input('campUrl');

        return view('mobile.order.verifyPayment1')->with('planDetail', $planDetail)
            ->with('billId', $request->input('out_trade_no'))
            ->with('campUrl', $campUrl);
    }

    public function getPayConfigs()
    {
        $options = $this->wxClient->getWxPayConfig();
        return new JsonResponse($options);
    }

    public static function largeLimit()
    {
        $dayRanges = [[15, 1, 2], ["end", -1, 2]];
        $nextDate = Carbon::now();
        $dayLeft = 100;
        $today = Carbon::now();

        foreach ($dayRanges as $dayRange) {
            if ($dayRange[1] > 0) {
                if ($dayRange[0] == 'end') {
                    $dayStart = Carbon::now()->endOfMonth();
                    $dayEnd = Carbon::now()->endOfMonth()->addDays($dayRange[2] - 1)->startOfDay();
                } else {
                    $dayStart = Carbon::create($today->year, $today->month, $dayRange[0])->startOfDay();
                    $dayEnd = $dayStart->copy()->addDays($dayRange[2] - 1)->endOfDay();
                }
            } else {
                if ($dayRange[0] == 'end') {
                    $dayStart = Carbon::now()->endOfMonth()->subDays($dayRange[2] - 1)->startOfDay();
                    $dayEnd = Carbon::now()->endOfMonth()->endOfDay();
                } else {
                    $dayEnd = Carbon::create($today->year, $today->month, $dayRange[0])->endOfDay();
                    $dayStart = $dayEnd->copy()->subDays($dayRange[2] - 1)->startOfDay();
                }

            }

            if ($today->between($dayStart, $dayEnd)) {
                return [
                    'day' => 0,
                    'next' => $today->format('m月d日 00:00'),
                    'start' => $dayStart,
                    'end' => $dayEnd,
                ];
            }
            if ($dayEnd->lt($today)) {
                continue;
            }
            $diffInSeconds = $today->diffInSeconds($dayStart);
            $diffInDays = ceil($diffInSeconds / 86400);
            if ($diffInDays < $dayLeft) {
                $dayLeft = $diffInDays;
                $nextDate = $dayStart->copy();
            }
        }

        return [
            'day' => $dayLeft,
            'next' => $nextDate->format('m月d日 00:00'),
        ];
    }

    //赠课活动
    public function bonusLesson(Request $request)
    {
        $signature = $this->wxClient->getJsSign($request->fullUrl());
        return view('mobile.campaign.bonusLesson')->with('signature', $signature);
    }

    //课后反馈
    public function feedback(Request $request, $appointmentId)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'appointmentId' => $appointmentId
        ];
        return view('mobile.feedback.feedback1')->with($paras);
    }

    //课后反馈
    public function feedback1(Request $request, $appointmentId)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'appointmentId' => $appointmentId
        ];
        return view('mobile.feedback.feedback1')->with($paras);
    }

    //课后反馈
    public function feedback2(Request $request, $appointmentId)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'appointmentId' => $appointmentId
        ];
        return view('mobile.feedback.feedback2')->with($paras);
    }

    //课后反馈
    public function feedback3(Request $request, $appointmentId)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'appointmentId' => $appointmentId
        ];
        return view('mobile.feedback.feedback3')->with($paras);
    }

    public function feedbackReview(Request $request, $appointmentId)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'appointmentId' => $appointmentId
        ];
        return view('mobile.feedback.review')->with($paras);
    }

    private function fetchProduct($id)
    {
        $url = env('BILLING_URL') . "/products/$id";
        $client = new Client();
        $response = $client->get($url, [
            'verify' => false,
            'headers' => [
                'apikey' => env('API_KEY'),
            ],
            'json' => [],
            'http_errors' => false
        ]);

        if ($response->getStatusCode() == 200) {
            return json_decode($response->getBody(), true);
        }

        return [];
    }

    // 双11 活动
    public function doubleEleven(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1003');
        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }
        $paras['largeLimit'] = self::largeLimit();
        return view('mobile.campaign.doubleEleven.groupBuy')->with($paras);
    }

    //圣诞活动
    public function christmas(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1004');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1005');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.christmas.groupBuy')->with($paras);
    }

    // 复活节活动
    public function easter(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1024');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1025');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.easter.groupBuy')->with($paras);
    }

    // 复活节活动
    public function easter1(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1024');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1025');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.easter1.groupBuy')->with($paras);
    }

    // 拼读课活动
    public function spell(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1026');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1027');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.spell.groupBuy')->with($paras);
    }

    // 拼读课活动
    public function phonics(Request $request, $num = 0)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1041');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1041');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'num' => $num,
            'share' => false
        ];

        return view('mobile.campaign.phonics.phonics')->with($paras);
    }

    // 拼读课活动
    public function phonicsShare(Request $request, $num = 0)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1041');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1041');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'num' => $num,
            'share' => true
        ];

        return view('mobile.campaign.phonics.phonics')->with($paras);
    }

    // 拼读课活动
    public function phonicsExtra(Request $request, $num = 0)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1043');
        //新增单独购买
        $extraCampaignProduct = Config::get('products.campaignProducts.1043');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'num' => $num
        ];

        return view('mobile.campaign.phonics.phonics1')->with($paras);
    }

    // 两人班活动
    public function two(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1032');
        $extraCampaignProduct = Config::get('products.campaignProducts.1033');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.two.groupBuy')->with($paras);
    }

    //寒假活动
    public function winterClass(Request $request, $num = 0)
    {
        $gid = $request->get('gid');
        $forceCreate = intval($request->get('fc', 0));

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1006');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        $paras['largeLimit'] = self::largeLimit();
        $paras['forceCreate'] = $forceCreate;
        return view('mobile.campaign.winterClass.groupBuy')->with($paras);
    }

    //寒假活动2
    public function winterClass2(Request $request, $num = 0)
    {
        $gid = $request->get('gid');
        $forceCreate = intval($request->get('fc', 0));

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1006');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        $paras['largeLimit'] = self::largeLimit();
        $paras['forceCreate'] = $forceCreate;
        return view('mobile.campaign.winterClass2.groupBuy')->with($paras);
    }

    //寒假活动3
    public function winterClass3(Request $request, $num = 0)
    {
        $gid = $request->get('gid');
        $forceCreate = intval($request->get('fc', 0));

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1006');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'gid' => '',
            'num' => $num
        ];

        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        $paras['largeLimit'] = self::largeLimit();
        $paras['forceCreate'] = $forceCreate;
        return view('mobile.campaign.winterClass3.groupBuy')->with($paras);
    }

    // 暑假活动(第一波)
    public function summer(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1034');
        $extraCampaignProduct = Config::get('products.campaignProducts.1035');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.summer.groupBuy')->with($paras);
    }

    // 暑假活动(第二波)
    public function summers(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1039');
        $extraCampaignProduct = Config::get('products.campaignProducts.1040');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.summer.groupBuy2')->with($paras);
    }

    // 暑假活动(单独购1)
    public function summer2s(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1044');
        $extraCampaignProduct = Config::get('products.campaignProducts.1044');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit(),
            'home' => true
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.summer.groupBuy3')->with($paras);
    }

    // 暑假活动(单独购2)
    public function summer3s(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1044');
        $extraCampaignProduct = Config::get('products.campaignProducts.1044');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit(),
            'home' => false
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.summer.groupBuy3')->with($paras);
    }

    // 昆虫课活动
    public function insect(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1037');
        $extraCampaignProduct = Config::get('products.campaignProducts.1038');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.insect.groupBuy')->with($paras);
    }

    // 昆虫课活动
    public function insect1(Request $request, $num = 0)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1037');
        $extraCampaignProduct = Config::get('products.campaignProducts.1038');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'num' => $num,
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.insect1.groupBuy')->with($paras);
    }

    private function getFullUrl(Request $request)
    {
        $requestUri = '';
        if (isset($_SERVER['REQUEST_URI'])) {
            $requestUri = $_SERVER['REQUEST_URI'];
        } else {
            if (isset($_SERVER['argv'])) {
                $requestUri = $_SERVER['PHP_SELF'] . '?' . $_SERVER['argv'][0];
            } else if (isset($_SERVER['QUERY_STRING'])) {
                $requestUri = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
            }
        }
        return $request->root() . $requestUri;
    }

    public function redeemcard(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'largeLimit' => self::largeLimit()
        ];

        return view('mobile.campaign.redeemcard.redeemcard')->with($paras);
    }

    public function calendarPresent(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature
        ];
        return view('mobile.campaign.calendarPresent.calendarPresent')->with($paras);
    }

    public function award(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature
        ];
        $paras['largeLimit'] = self::largeLimit();
        return view('mobile.campaign.award.award')->with($paras);
    }

    public function classReport()
    {
        return view('mobile.report.class');
    }

    public function unitReport()
    {
        return view('mobile.report.unit');
    }

    public function upgrade()
    {
        return view('mobile.campaign.upgrade.upgrade');
    }

    // 打卡页面
    public function achievementClocks(Request $request, $clockId = 0)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'clockId' => $clockId
        ];
        return view('mobile.campaign.achievement.clocks')->with($paras);
    }

    // 分享出去的打卡页面
    public function achievementClocksShare(Request $request, $clockId = 0)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature,
            'clockId' => $clockId
        ];
        return view('mobile.campaign.achievement.clocksShare')->with($paras);
    }

    // 查看自己当前任务
    public function achievement()
    {
        return view('mobile.campaign.achievement.achievement');
    }

    // 设定目标页面
    public function newAchievement()
    {
        return view('mobile.campaign.achievement.new');
    }

    // 久趣英语服务号
    public function subscribe(Request $request)
    {
        $paras = [
            'signature' => $this->wxClient->getJsSign($this->getFullUrl($request))
        ];

        return view('mobile.campaign.subscribe.subscribe')->with($paras);
    }

    public function logError(Request $request)
    {
        Log::debug(json_encode($request->all()));
    }

    // 老用户邀请新用户 赠一得二
    public function invite(Request $request)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1037');
        $extraCampaignProduct = Config::get('products.campaignProducts.1038');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.invite.intro')->with($paras);
    }

    // 老用户邀请新用户 赠一得二分享页
    public function inviteShare(Request $request)
    {
        $gid = $request->get('gid');

        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1037');
        $extraCampaignProduct = Config::get('products.campaignProducts.1038');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'gid' => '',
            'largeLimit' => self::largeLimit()
        ];
        if ($gid != '') {
            $paras['gid'] = $gid;
        }

        return view('mobile.campaign.invite.share')->with($paras);
    }

    // 优学派课时兑换
    public function yxpRedeem(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'largeLimit' => self::largeLimit()
        ];

        return view('mobile.campaign.yxp.redeem')->with($paras);
    }

    // 优学派优惠券兑换
    public function yxpCoupon(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');

        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => 1048,
            'largeLimit' => self::largeLimit(),
            'recommendPlan' => 4,
            'buyOption' => 0
        ];


        $fromDate = Carbon::createFromFormat('Y-m-d', '2016-01-01');
//
        $paras['batch'][4] = Carbon::now()->startOfWeek()->diffInWeeks($fromDate->startOfWeek());
        $product = $this->fetchProduct(4);
        if (!empty($product)) {
            $paras['product'][4] = [
                'id' => $product['id'],
                'name' => $product['name'],
                'left' => max(22, 500 - max(100, $product['totalSoldCount'] % 500))
            ];
            $product = '';
        }

        $paras['batch'][6] = Carbon::now()->startOfMonth()->diffInMonths($fromDate->startOfMonth()) * 2 + intval(Carbon::now()->day > 16);
        $product = $this->fetchProduct(6);
        if (!empty($product)) {
            $paras['product'][6] = [
                'id' => $product['id'],
                'name' => $product['name'],
                'left' => max(22, 500 - max(100, $product['totalSoldCount'] % 500))
            ];
        }

        return view('mobile.campaign.yxp.coupon')->with($paras);
    }

    // 地推人员
    public function salesMedia(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature
        ];
        return view('mobile.sales.media')->with($paras);
    }

    // 微信朋友圈推广购买详情页
    public function friend(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        if (strpos(Agent::getUserAgent(),'MicroMessenger')) {
            $code = $request->get('code');
            $channel = $request->get('_c');
            $clickId = $request->get('gdt_vid');
            $redirectUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . config("weChat.appid") . '&redirect_uri=' . $this->getFullUrl($request) . '&state=' . $clickId . '+++' . $channel . '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
            if (empty($code)) return redirect($redirectUrl);
        }
        $paras = [
            'signature' => $signature,
            'productId' => 4,
        ];
        return view('mobile.campaign.friend.friend')->with($paras);
    }

    public function assistant(Request $request, $name)
    {
        return view('mobile.assistant')->with([
            'name' => $name,
        ]);
    }

    // 1元8人抽奖
    public function lottery(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $products = Config::get('products.products');
        $campaignProduct = Config::get('products.campaignProducts.1056');
        $extraCampaignProduct = Config::get('products.campaignProducts.1056');
        $paras = [
            'signature' => $signature,
            'products' => $products,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
            'largeLimit' => $this->largeLimit(),
        ];
        return view('mobile.campaign.lottery.lottery')->with($paras);
    }

    public function lotteryResult(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1056');
        $extraCampaignProduct = Config::get('products.campaignProducts.1056');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
        ];
        return view('mobile.campaign.lottery.result')->with($paras);
    }

    public function one(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1057');
        $extraCampaignProduct = Config::get('products.campaignProducts.1057');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
        ];
        return view('mobile.campaign.1v1.1v1')->with($paras);
    }

    public function one1(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1058');
        $extraCampaignProduct = Config::get('products.campaignProducts.1058');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
        ];
        return view('mobile.campaign.1v1.1v1-1')->with($paras);
    }

    public function one2(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $campaignProduct = Config::get('products.campaignProducts.1061');
        $extraCampaignProduct = Config::get('products.campaignProducts.1061');
        $paras = [
            'signature' => $signature,
            'campaignProduct' => $campaignProduct,
            'extraCampaignProduct' => $extraCampaignProduct,
        ];
        return view('mobile.campaign.1v1.1v1-2')->with($paras);
    }

    public function order(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        if (strpos(Agent::getUserAgent(),'MicroMessenger')) {
            $code = $request->get('code');
            $channel = $request->get('_c');
            $productId = $request->get('pid');
            $redirectUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . config("weChat.appid") . '&redirect_uri=' . $this->getFullUrl($request) . '&state=' . $productId . '+++' . $channel . '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
            if (empty($code)) return redirect($redirectUrl);
        }
        $paras = [
            'signature' => $signature,
        ];
        return view('mobile.campaign.order.order')->with($paras);
    }

    public function order1(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        if (strpos(Agent::getUserAgent(),'MicroMessenger')) {
            $code = $request->get('code');
            $channel = $request->get('_c');
            $productId = $request->get('pid');
            $redirectUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . config("weChat.appid") . '&redirect_uri=' . $this->getFullUrl($request) . '&state=' . $productId . '+++' . $channel . '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
            if (empty($code)) return redirect($redirectUrl);
        }
        $paras = [
            'signature' => $signature,
        ];
        return view('mobile.campaign.order.order1')->with($paras);
    }

    public function order3(Request $request)
    {
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        if (strpos(Agent::getUserAgent(),'MicroMessenger')) {
            $code = $request->get('code');
            $channel = $request->get('_c');
            $productId = $request->get('pid');
            $redirectUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . config("weChat.appid") . '&redirect_uri=' . $this->getFullUrl($request) . '&state=' . $productId . '+++' . $channel . '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
            if (empty($code)) return redirect($redirectUrl);
        }
        $paras = [
            'signature' => $signature,
        ];
        return view('mobile.campaign.order.order3')->with($paras);
    }

    public function verify(Request $request)
    {
        $productId = $request->get('pid');
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature
        ];
        if ($productId === '1057') return view('mobile.campaign.verify.verify')->with($paras);
        else if ($productId === '1058') return view('mobile.campaign.verify.verify1')->with($paras);
        else if ($productId === '1061') return view('mobile.campaign.verify.verify2')->with($paras);
        else return view('mobile.campaign.verify.verify3')->with($paras);
		}

		public function verify10(Request $request)
    {
        $productId = $request->get('pid');
        $signature = $this->wxClient->getJsSign($this->getFullUrl($request));
        $paras = [
            'signature' => $signature
        ];
        return view('mobile.campaign.verify.verify10')->with($paras);
    }

    public function luckyBag(Request $request)
    {
        return view('mobile.campaign.luckyBag.luckyBag')->with([
            'signature' => $this->wxClient->getJsSign($this->getFullUrl($request))
        ]);
    }

    public function tenTimesLesson(Request $request)
    {
        return view('mobile.campaign.tenTimesLesson.tenTimesLesson')->with([
            'signature' => $this->wxClient->getJsSign($this->getFullUrl($request))
        ]);
    }


    public function zeroThree(Request $request)
    {
        return view('mobile.campaign.zeroThree.zeroThree')->with([
            'signature' => $this->wxClient->getJsSign($this->getFullUrl($request)),
            'campaignProduct' => Config::get('products.campaignProducts.1062'),
            'extraCampaignProduct' => Config::get('products.campaignProducts.1062')
        ]);
    }

    public function inviteOne(Request $request)
    {
        return view('mobile.campaign.inviteOne.invite')->with([
            'signature' => $this->wxClient->getJsSign($this->getFullUrl($request)),
            'campaignProduct' => Config::get('products.campaignProducts.1063'),
            'extraCampaignProduct' => Config::get('products.campaignProducts.1063')
        ]);
    }

    public function inviteOneResult(Request $request)
    {
        return view('mobile.campaign.inviteOne.result')->with([
            'signature' => $this->wxClient->getJsSign($this->getFullUrl($request)),
            'campaignProduct' => Config::get('products.campaignProducts.1063'),
            'extraCampaignProduct' => Config::get('products.campaignProducts.1063')
        ]);
    }
}
